package config

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func Mysql() *gorm.DB {
	config := GetConfig()
	username := config.Datasource.Mysql.Username //账号
	password := config.Datasource.Mysql.Password //密码
	host := config.Datasource.Mysql.Host         //数据库地址，可以是Ip或者域名
	port := config.Datasource.Mysql.Port         //数据库端口
	Dbname := config.Datasource.Mysql.Dbname     //数据库名
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host, port, Dbname)
	Mysql, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("连接数据库失败, error=" + err.Error())
	}
	return Mysql
}
